<template>
  <div v-if="visible" class="edit-dialog-overlay" @click="handleOverlayClick">
    <div class="edit-dialog" @click.stop>
      <div class="edit-dialog-header">
        <h3>编辑节点内容</h3>
        <button @click="handleClose" class="close-btn">×</button>
      </div>
      <div class="edit-dialog-body">
        <textarea 
          ref="textarea"
          v-model="localContent" 
          class="edit-textarea"
          placeholder="请输入Markdown内容..."
          @keydown.ctrl.s.prevent="handleSave"
          @keydown.esc="handleClose"
        ></textarea>
      </div>
      <div class="edit-dialog-footer">
        <button @click="handleClose" class="cancel-btn">取消</button>
        <button @click="handleSave" class="save-btn">保存</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'NodeEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    content: {
      type: String,
      default: ''
    },
    nodeId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      localContent: ''
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.localContent = this.content
        this.$nextTick(() => {
          if (this.$refs.textarea) {
            this.$refs.textarea.focus()
          }
        })
      }
    },
    content(newVal) {
      this.localContent = newVal
    }
  },
  methods: {
    handleSave() {
      this.$emit('save', {
        nodeId: this.nodeId,
        content: this.localContent
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleOverlayClick() {
      this.handleClose()
    }
  }
}
</script>

<style scoped>
.edit-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.edit-dialog {
  background: white;
  border-radius: 8px;
  width: 600px;
  max-width: 90vw;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.edit-dialog-header {
  padding: 16px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.edit-dialog-header h3 {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.edit-dialog-body {
  padding: 16px;
  flex: 1;
}

.edit-textarea {
  width: 100%;
  height: 300px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 12px;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.5;
  resize: vertical;
  outline: none;
  transition: border-color 0.2s;
}

.edit-textarea:focus {
  border-color: #007bff;
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

.edit-dialog-footer {
  padding: 16px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.save-btn {
  background: #007bff;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
}

.save-btn:hover {
  background: #0056b3;
}

.cancel-btn {
  background: #6c757d;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
}

.cancel-btn:hover {
  background: #545b62;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.close-btn:hover {
  background: #f5f5f5;
  color: #666;
}
</style>